package edu.scau.mis.test.cglib;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * Cglib实现动态代理
 * 目标对象可以不用实现接口
 */
public class DynamicProxyOfCglibInterceptor implements MethodInterceptor {
    private Object target;
    public Object getInstance(final Object target) {
        this.target = target;
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(this.target.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("代理联系业主讨价还价");
        Object result = methodProxy.invokeSuper(o, objects);
        System.out.println("代理联系业主办理过户");
        return result;
    }
}
